
<!DOCTYPE html>
<html lang="zh-Hans" class="loading">
<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>后HTML5时代 - 默默默默燃</title>
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <meta name="google" content="notranslate" />
    <meta name="keywords" content="TriDiamond Obsidian,"> 
    <meta name="description" content="一枚前端搬砖队队员的记录册,HTML5 Device API原文地址
让音乐随心而动 - 音频处理 Web audio APIAudio对象提供的只是音频文件的播放，而Web Audio则是给了开发者对音频数据进行分析、处理的,"> 
    <meta name="author" content="张白告丶"> 
    <link rel="alternative" href="atom.xml" title="默默默默燃" type="application/atom+xml"> 
    <link rel="icon" href="/img/favicon.png"> 
    <link href="https://fonts.loli.net/css?family=Roboto+Mono|Rubik&display=swap" rel="stylesheet">
    
<link rel="stylesheet" href="//at.alicdn.com/t/font_1429596_nzgqgvnmkjb.css">

    
<link rel="stylesheet" href="//cdn.bootcss.com/animate.css/3.7.2/animate.min.css">

    
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/social-share.js/1.0.16/css/share.min.css">

    
<link rel="stylesheet" href="//cdn.bootcss.com/codemirror/5.48.4/codemirror.min.css">

    
<link rel="stylesheet" href="//cdn.bootcss.com/codemirror/5.48.4/theme/dracula.css">

    
<link rel="stylesheet" href="/css/obsidian.css">

    
<link rel="stylesheet" href="/css/ball-atom.min.css">

<meta name="generator" content="Hexo 4.2.0"></head>


<body class="loading">
    <div class="loader">
        <div class="la-ball-atom la-2x">
            <div></div>
            <div></div>
            <div></div>
            <div></div>
        </div>
    </div>
    <span id="config-title" style="display:none">默默默默燃</span>
    <div id="loader"></div>
    <div id="single">
    <div class="scrollbar gradient-bg-rev"></div>
<div id="top" style="display: block;">
    <div class="bar" style="width: 0;"></div>
    <div class="navigation animated fadeIn fast delay-1s">
        <img id="home-icon" class="icon-home" src="/img/favicon.png" alt="" data-url="https://zhanghao-web.github.io">
        <div id="play-icon" title="Play/Pause" class="iconfont icon-play"></div>
        <h3 class="subtitle">后HTML5时代</h3>
        <div class="social">
            <!--        <div class="like-icon">-->
            <!--            <a href="javascript:;" class="likeThis active"><span class="icon-like"></span><span class="count">76</span></a>-->
            <!--        </div>-->
            <div>
                <div class="share">
                    
                        <a href="javascript:;" class="iconfont icon-share1"></a>
                        <div class="share-component-cc" data-disabled="facebook,douban,linkedin,diandian,tencent,google"></div>
                    
                </div>
            </div>
        </div>
    </div>
</div>

    <div class="section">
        <div class=article-header-wrapper>
    <div class="article-header">
        <div class="article-cover animated fadeIn" style="
            animation-delay: 600ms;
            animation-duration: 1.2s;
            background-image: 
                radial-gradient(ellipse closest-side, rgba(0, 0, 0, 0.65), #100e17),
                url(/img/cover.jpg) ">
        </div>
        <div class="else">
            <p class="animated fadeInDown">
                
                <a href="/categories/HTML5"><b>「
                    </b>HTML5<b> 」</b></a>
                
                February 23, 2018
            </p>
            <h3 class="post-title animated fadeInDown"><a href="/2018/02/23/HTML5/%E5%90%8EHTML5%E6%97%B6%E4%BB%A3/" title="后HTML5时代" class="">后HTML5时代</a>
            </h3>
            
            <p class="post-count animated fadeInDown">
                
                <span>
                    <b class="iconfont icon-text2"></b> <i>Words count</i>
                    37k
                </span>
                
                
                <span>
                    <b class="iconfont icon-timer__s"></b> <i>Reading time</i>
                    34 mins.
                </span>
                
                
                
                <span id="busuanzi_container_page_pv">
                    <b class="iconfont icon-read"></b> <i>Read count</i>
                    <span id="busuanzi_value_page_pv">0</span>
                </span>
                
            </p>
            
            
            <ul class="animated fadeInDown post-tags-list" itemprop="keywords"><li class="animated fadeInDown post-tags-list-item"><a class="animated fadeInDown post-tags-list-link" href="/tags/HTML5/" rel="tag">HTML5</a></li></ul>
            
        </div>
    </div>
</div>

<div class="screen-gradient-after">
    <div class="screen-gradient-content">
        <div class="screen-gradient-content-inside">
            <div class="bold-underline-links screen-gradient-sponsor">
                <p>
                    <span class="animated fadeIn delay-1s"></span>
                </p>
            </div>
        </div>
    </div>
</div>

<div class="article">
    <div class='main'>
        <div class="content markdown animated fadeIn">
            <h1 id="HTML5-Device-API"><a href="#HTML5-Device-API" class="headerlink" title="HTML5 Device API"></a>HTML5 Device API</h1><p><a href="http://tgideas.qq.com/webplat/info/news_version3/804/7104/7106/m5723/201506/354489.shtml" target="_blank" rel="noopener">原文地址</a></p>
<h2 id="让音乐随心而动-音频处理-Web-audio-API"><a href="#让音乐随心而动-音频处理-Web-audio-API" class="headerlink" title="让音乐随心而动 - 音频处理 Web audio API"></a>让音乐随心而动 - 音频处理 Web audio API</h2><p>Audio对象提供的只是音频文件的播放，而Web Audio则是给了开发者对音频数据进行分析、处理的能力，比如混音、过滤。</p>
<blockquote>
<p>系统要求 :<br>ios6+、android chrome、android firefox</p>
</blockquote>
<p><strong>核心代码</strong> :</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> context = <span class="keyword">new</span> webkitAudioContext();</span><br><span class="line"><span class="keyword">var</span> source = context.createBufferSource();   <span class="comment">// 创建一个声音源</span></span><br><span class="line">source.buffer = buffer;   <span class="comment">// 告诉该源播放何物 </span></span><br><span class="line">createBufferSourcesource.connect(context.destination);   <span class="comment">// 将该源与硬件相连</span></span><br><span class="line">source.start(<span class="number">0</span>); <span class="comment">//播放</span></span><br></pre></td></tr></table></figure>
<a id="more"></a>
<p>技术分析 :</p>
<p>当我们加载完音频数据后，我们将创建一个全局的AudioContext对象来对音频进行处理，AudioContext可以创建各种不同功能类型的音频节点AudioNode</p>
<h3 id="1-源节点（source-node）"><a href="#1-源节点（source-node）" class="headerlink" title="1.源节点（source node）"></a>1.源节点（source node）</h3><h4 id="1-audio标签"><a href="#1-audio标签" class="headerlink" title="1.audio标签"></a>1.audio标签</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> sound, audio = <span class="keyword">new</span> Audio();</span><br><span class="line">audio.addEventListener(<span class="string">'canplay'</span>, <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line"> sound = context.createMediaElementSource(audio);</span><br><span class="line"> sound.connect(context.destination);</span><br><span class="line">&#125;);</span><br><span class="line">audio.src = <span class="string">'/audio.mp3'</span>;</span><br></pre></td></tr></table></figure>
<h4 id="2-XMLHttpRequest"><a href="#2-XMLHttpRequest" class="headerlink" title="2.XMLHttpRequest"></a>2.XMLHttpRequest</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> sound, context = createAudioContext();</span><br><span class="line"><span class="keyword">var</span> audioURl = <span class="string">'/audio.mp3'</span>; <span class="comment">// 音频文件URL</span></span><br><span class="line"><span class="keyword">var</span> xhr = <span class="keyword">new</span> XMLHttpRequest();</span><br><span class="line">xhr.open(<span class="string">'GET'</span>, audioURL, <span class="literal">true</span>);</span><br><span class="line">xhr.responseType = <span class="string">'arraybuffer'</span>; </span><br><span class="line">xhr.onload = <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line"> context.decodeAudioData(request.response, <span class="function"><span class="keyword">function</span> (<span class="params">buffer</span>) </span>&#123;</span><br><span class="line"> source = context.createBufferSource();</span><br><span class="line"> source.buffer = buffer;</span><br><span class="line"> source.connect(context.destination);</span><br><span class="line"> &#125;</span><br><span class="line">&#125;</span><br><span class="line">xhr.send();</span><br></pre></td></tr></table></figure>
<h3 id="2-分析节点（analyser-node）"><a href="#2-分析节点（analyser-node）" class="headerlink" title="2.分析节点（analyser node）"></a>2.分析节点（analyser node）</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> audioCtx = <span class="keyword">new</span> (<span class="built_in">window</span>.AudioContext || <span class="built_in">window</span>.webkitAudioContext)();</span><br><span class="line"><span class="keyword">var</span> analyser = audioCtx.createAnalyser();</span><br><span class="line">analyser.fftSize = <span class="number">2048</span>;</span><br><span class="line"><span class="keyword">var</span> bufferLength = analyser.frequencyBinCount;</span><br><span class="line"><span class="keyword">var</span> dataArray = <span class="keyword">new</span> <span class="built_in">Uint8Array</span>(bufferLength);</span><br><span class="line">analyser.getByteTimeDomainData(dataArray);</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">draw</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line"> drawVisual = requestAnimationFrame(draw);</span><br><span class="line"> analyser.getByteTimeDomainData(dataArray);</span><br><span class="line"> <span class="comment">// 将dataArray数据以canvas方式渲染出来</span></span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">draw();</span><br></pre></td></tr></table></figure>
<h3 id="3-处理节点（gain-node、panner-node、wave-shaper-node、delay-node、convolver-node等）"><a href="#3-处理节点（gain-node、panner-node、wave-shaper-node、delay-node、convolver-node等）" class="headerlink" title="3.处理节点（gain node、panner node、wave shaper node、delay node、convolver node等）"></a>3.处理节点（gain node、panner node、wave shaper node、delay node、convolver node等）</h3><p>不同的处理节点有不同的作用，比如使用BiquadFilterNode调整音色（大量滤波器）、使用ChannelSplitterNode分割左右声道、使用GainNode调整增益值实现音乐淡入淡出等等。需要了解更多的音频节点可能参考：<br><a href="https://developer.mozilla.org/en-US/docs/Web/API/Web_Audio_API" target="_blank" rel="noopener">https://developer.mozilla.org/en-US/docs/Web/API/Web_Audio_API</a></p>
<h3 id="4-目的节点（destination-node）"><a href="#4-目的节点（destination-node）" class="headerlink" title="4.目的节点（destination node）"></a>4.目的节点（destination node）</h3><p>所有被渲染音频流到达的最终地点</p>
<h2 id="捕捉用户摄像头-媒体流-Media-Capture"><a href="#捕捉用户摄像头-媒体流-Media-Capture" class="headerlink" title="捕捉用户摄像头 - 媒体流 Media Capture"></a>捕捉用户摄像头 - 媒体流 Media Capture</h2><p>通过getUserMedia捕捉用户摄像头获取视频流和通过麦克风获取用户声音。</p>
<blockquote>
<p>系统要求 :<br>android chrome、android firefox</p>
</blockquote>
<p><strong>核心代码</strong> :</p>
<h3 id="1、摄像头捕捉"><a href="#1、摄像头捕捉" class="headerlink" title="1、摄像头捕捉"></a>1、摄像头捕捉</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">navigator.webkitGetUserMedia (&#123;<span class="attr">video</span>: <span class="literal">true</span>&#125;, <span class="function"><span class="keyword">function</span>(<span class="params">stream</span>) </span>&#123;</span><br><span class="line"> video.src = <span class="built_in">window</span>.URL.createObjectURL(stream);</span><br><span class="line"> localMediaStream = stream;</span><br><span class="line">&#125;, <span class="function"><span class="keyword">function</span>(<span class="params">e</span>)</span>&#123;</span><br><span class="line"></span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<h3 id="2、从视频流中拍照"><a href="#2、从视频流中拍照" class="headerlink" title="2、从视频流中拍照"></a>2、从视频流中拍照</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">btnCapture.addEventListener(<span class="string">'touchend'</span>, <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">	<span class="keyword">if</span> (localMediaStream) &#123;</span><br><span class="line">		canvas.setAttribute(<span class="string">'width'</span>, video.videoWidth);</span><br><span class="line">		canvas.setAttribute(<span class="string">'height'</span>, video.videoHeight);</span><br><span class="line">		ctx.drawImage(video, <span class="number">0</span>, <span class="number">0</span>);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;, <span class="literal">false</span>);</span><br></pre></td></tr></table></figure>
<h3 id="3、用户声音录制"><a href="#3、用户声音录制" class="headerlink" title="3、用户声音录制"></a>3、用户声音录制</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">navigator.getUserMedia(&#123;<span class="attr">audio</span>:<span class="literal">true</span>&#125;, <span class="function"><span class="keyword">function</span>(<span class="params">e</span>) </span>&#123;</span><br><span class="line">	context = <span class="keyword">new</span> audioContext();</span><br><span class="line">	audioInput = context.createMediaStreamSource(e);	</span><br><span class="line">	volume = context.createGain();</span><br><span class="line">	recorder = context.createScriptProcessor(<span class="number">2048</span>, <span class="number">2</span>, <span class="number">2</span>);</span><br><span class="line">	recorder.onaudioprocess = <span class="function"><span class="keyword">function</span>(<span class="params">e</span>)</span>&#123;</span><br><span class="line">		recordingLength += <span class="number">2048</span>;</span><br><span class="line">		recorder.connect (context.destination); </span><br><span class="line">	&#125;	</span><br><span class="line">&#125;, <span class="function"><span class="keyword">function</span>(<span class="params">error</span>)</span>&#123;&#125;);</span><br></pre></td></tr></table></figure>
<h3 id="4、保存用户录制的声音"><a href="#4、保存用户录制的声音" class="headerlink" title="4、保存用户录制的声音"></a>4、保存用户录制的声音</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> buffer = <span class="keyword">new</span> <span class="built_in">ArrayBuffer</span>(<span class="number">44</span> + interleaved.length * <span class="number">2</span>);</span><br><span class="line"><span class="keyword">var</span> view = <span class="keyword">new</span> <span class="built_in">DataView</span>(buffer);</span><br><span class="line">fileReader.readAsDataURL(blob); <span class="comment">// android chrome audio不支持blob</span></span><br><span class="line">… audio.src = event.target.result;</span><br></pre></td></tr></table></figure>
<h2 id="你是逗逼？-语音识别-Web-Speech-API"><a href="#你是逗逼？-语音识别-Web-Speech-API" class="headerlink" title="你是逗逼？ - 语音识别 Web Speech API"></a>你是逗逼？ - 语音识别 Web Speech API</h2><p>将文本转换成语音；将语音识别为文本。</p>
<blockquote>
<p>系统要求 :<br>ios7+，android chrome，android firefox</p>
</blockquote>
<p><strong>核心代码</strong> :</p>
<h3 id="1、文本转换成语音，使用SpeechSynthesisUtterance对象"><a href="#1、文本转换成语音，使用SpeechSynthesisUtterance对象" class="headerlink" title="1、文本转换成语音，使用SpeechSynthesisUtterance对象."></a>1、文本转换成语音，使用SpeechSynthesisUtterance对象.</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> msg = <span class="keyword">new</span> SpeechSynthesisUtterance();</span><br><span class="line"><span class="keyword">var</span> voices = <span class="built_in">window</span>.speechSynthesis.getVoices();</span><br><span class="line">msg.volume = <span class="number">1</span>; <span class="comment">// 0 to 1</span></span><br><span class="line">msg.text = ‘识别的文本内容’;</span><br><span class="line">msg.lang = <span class="string">'en-US'</span>;</span><br><span class="line">speechSynthesis.speak(msg);</span><br></pre></td></tr></table></figure>
<h3 id="2、语音转换为文本，使用SpeechRecognition对象"><a href="#2、语音转换为文本，使用SpeechRecognition对象" class="headerlink" title="2、语音转换为文本，使用SpeechRecognition对象."></a>2、语音转换为文本，使用SpeechRecognition对象.</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="keyword">var</span> newRecognition = <span class="keyword">new</span> webkitSpeechRecognition();</span><br><span class="line">newRecognition.onresult = <span class="function"><span class="keyword">function</span>(<span class="params">event</span>)</span>&#123;</span><br><span class="line">	<span class="keyword">var</span> interim_transcript = <span class="string">''</span>; </span><br><span class="line">	<span class="keyword">for</span> (<span class="keyword">var</span> i = event.resultIndex; i &lt; event.results.length; ++i) &#123;</span><br><span class="line">		final_transcript += event.results[i][<span class="number">0</span>].transcript;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<p>测试结论：<br>Android支持不稳定；语音识别测试失败（暂且认为是某些内置接口被墙所致）。</p>
<h2 id="让我尽情呵护你-设备电量-Battery-API"><a href="#让我尽情呵护你-设备电量-Battery-API" class="headerlink" title="让我尽情呵护你 - 设备电量 Battery API"></a>让我尽情呵护你 - 设备电量 Battery API</h2><p>查询用户设备电量及是否正在充电。</p>
<blockquote>
<p>系统要求 :<br>android firefox</p>
</blockquote>
<p><strong>核心代码</strong> ：<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> battery = navigator.battery || navigator.webkitBattery || navigator.mozBattery || navigator.msBattery;</span><br><span class="line"><span class="keyword">var</span> str = <span class="string">''</span>;</span><br><span class="line"><span class="keyword">if</span> (battery) &#123;</span><br><span class="line"> str += <span class="string">'&lt;p&gt;你的浏览器支持HTML5 Battery API&lt;/p&gt;'</span>;</span><br><span class="line"> <span class="keyword">if</span>(battery.charging) &#123;</span><br><span class="line"> str += <span class="string">'&lt;p&gt;你的设备正在充电&lt;/p&gt;'</span>;</span><br><span class="line">&#125; <span class="keyword">else</span> &#123;</span><br><span class="line"> str += <span class="string">'&lt;p&gt;你的设备未处于充电状态&lt;/p&gt;'</span>;</span><br><span class="line">&#125;</span><br><span class="line"> str += <span class="string">'&lt;p&gt;你的设备剩余'</span>+ <span class="built_in">parseInt</span>(battery.level*<span class="number">100</span>)+<span class="string">'%的电量&lt;/p&gt;'</span>;</span><br><span class="line">&#125; <span class="keyword">else</span> &#123;</span><br><span class="line"> str += <span class="string">'&lt;p&gt;你的浏览器不支持HTML5 Battery API&lt;/p&gt;'</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>测试结论 ：<br>1、QQ浏览器与UC浏览器支持该接口，但未正确显示设备电池信息；<br>2、caniuse显示android chrome42支持该接口，实测不支持。</p>
<h2 id="获取用户位置-地理位置-Geolocation-API"><a href="#获取用户位置-地理位置-Geolocation-API" class="headerlink" title="获取用户位置 - 地理位置 Geolocation API"></a>获取用户位置 - 地理位置 Geolocation API</h2><p>Geolocation API用于将用户当前地理位置信息共享给信任的站点，目前主流移动设备都能够支持。</p>
<blockquote>
<p>系统要求 :<br>ios6+、android2.3+</p>
</blockquote>
<p><strong>核心代码</strong>：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> domInfo = $(<span class="string">"#info"</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 获取位置坐标</span></span><br><span class="line"><span class="keyword">if</span> (navigator.geolocation) &#123;</span><br><span class="line">	navigator.geolocation.getCurrentPosition(showPosition,showError);</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">else</span>&#123;</span><br><span class="line">	domInfo.innerHTML=<span class="string">"抱歉，你的浏览器不支持地理定位！"</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 使用腾讯地图显示位置</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">showPosition</span>(<span class="params">position</span>) </span>&#123;</span><br><span class="line">	<span class="keyword">var</span> lat=position.coords.latitude;</span><br><span class="line">	<span class="keyword">var</span> lon=position.coords.longitude;</span><br><span class="line"></span><br><span class="line">	mapholder = $(<span class="string">'#mapholder'</span>)</span><br><span class="line">	mapholder.style.height=<span class="string">'250px'</span>;</span><br><span class="line">	mapholder.style.width = <span class="built_in">document</span>.documentElement.clientWidth + <span class="string">'px'</span>;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">var</span> center = <span class="keyword">new</span> soso.maps.LatLng(lat, lon);</span><br><span class="line">	<span class="keyword">var</span> map = <span class="keyword">new</span> soso.maps.Map(mapholder,&#123;</span><br><span class="line">		center: center,</span><br><span class="line">		zoomLevel: <span class="number">13</span></span><br><span class="line">	&#125;);</span><br><span class="line"></span><br><span class="line">	<span class="keyword">var</span> geolocation = <span class="keyword">new</span> soso.maps.Geolocation();</span><br><span class="line">	<span class="keyword">var</span> marker = <span class="literal">null</span>;</span><br><span class="line">	geolocation.position(&#123;&#125;, <span class="function"><span class="keyword">function</span>(<span class="params">results, status</span>) </span>&#123;</span><br><span class="line">		<span class="built_in">console</span>.log(results);</span><br><span class="line">		<span class="keyword">var</span> city = $(<span class="string">"#info"</span>);</span><br><span class="line">		<span class="keyword">if</span> (status == soso.maps.GeolocationStatus.OK) &#123;</span><br><span class="line">			map.setCenter(results.latLng);</span><br><span class="line">			domInfo.innerHTML = <span class="string">'你当前所在城市: '</span> + results.name;</span><br><span class="line">		<span class="keyword">if</span> (marker != <span class="literal">null</span>) &#123;</span><br><span class="line">			marker.setMap(<span class="literal">null</span>);</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="comment">// 设置标记</span></span><br><span class="line">		marker = <span class="keyword">new</span> soso.maps.Marker(&#123;</span><br><span class="line">			map: map,</span><br><span class="line">			position:results.latLng</span><br><span class="line">		&#125;);</span><br><span class="line">		&#125; <span class="keyword">else</span> &#123;</span><br><span class="line">			alert(<span class="string">"检索没有结果，原因: "</span> + status);</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>测试结论：</p>
<p>1、Geolocation API的位置信息来源包括GPS、IP地址、RFID、WIFI和蓝牙的MAC地址、以及GSM/CDMS的ID等等。规范中没有规定使用这些设备的先后顺序。<br>2、初测3g环境下比wifi环境理定位更准确；<br>3、测试三星 GT-S6358(android2.3) geolocation存在，但显示位置信息不可用POSITION_UNAVAILABLE。</p>
<h2 id="把用户捧在手心-环境光-Ambient-Light-API"><a href="#把用户捧在手心-环境光-Ambient-Light-API" class="headerlink" title="把用户捧在手心 - 环境光 Ambient Light API"></a>把用户捧在手心 - 环境光 Ambient Light API</h2><p>Ambient Light API定义了一些事件，这些时间可以提供源于周围光亮程度的信息，这通常是由设备的光感应器来测量的。设备的光感应器会提取出辉度信息。</p>
<blockquote>
<p>系统要求 :<br>android firefox</p>
</blockquote>
<p><strong>核心代码</strong>：<br>这段代码实现感应用前当前环境光强度，调整网页背景和文字颜色。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> domInfo = $(<span class="string">'#info'</span>);</span><br><span class="line"><span class="keyword">if</span> (!(<span class="string">'ondevicelight'</span> <span class="keyword">in</span> <span class="built_in">window</span>)) &#123;</span><br><span class="line">	domInfo.innerHTML = <span class="string">'你的设备不支持环境光Ambient Light API'</span>;</span><br><span class="line">&#125; <span class="keyword">else</span> &#123;</span><br><span class="line">	<span class="keyword">var</span> lightValue = <span class="built_in">document</span>.getElementById(<span class="string">'dl-value'</span>);</span><br><span class="line">	<span class="built_in">window</span>.addEventListener(<span class="string">'devicelight'</span>, <span class="function"><span class="keyword">function</span>(<span class="params">event</span>) </span>&#123;</span><br><span class="line">		domInfo.innerHTML = <span class="string">'当前环境光线强度为：'</span> + <span class="built_in">Math</span>.round(event.value) + <span class="string">'lux'</span>;</span><br><span class="line">		<span class="keyword">var</span> backgroundColor = <span class="string">'rgba(0,0,0,'</span>+(<span class="number">1</span>-event.value/<span class="number">100</span>) +<span class="string">')'</span>;</span><br><span class="line">		<span class="built_in">document</span>.body.style.backgroundColor = backgroundColor;</span><br><span class="line">		<span class="keyword">if</span>(event.value &lt; <span class="number">50</span>) &#123;</span><br><span class="line">			<span class="built_in">document</span>.body.style.color = <span class="string">'#fff'</span></span><br><span class="line">		&#125; <span class="keyword">else</span> &#123;</span><br><span class="line">			<span class="built_in">document</span>.body.style.color = <span class="string">'#000'</span></span><br><span class="line">		&#125;</span><br><span class="line">	&#125;);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="陀螺仪-Deviceorientation"><a href="#陀螺仪-Deviceorientation" class="headerlink" title="陀螺仪 Deviceorientation"></a>陀螺仪 Deviceorientation</h2><p>装逼说法：设备移动传感器。</p>
<p>通俗说法：检测设备运动及运动状况，可以通过该接口获取到设备运动的方向和速度等相关信息</p>
<p><strong>核心代码</strong> :<br>这货其实就是一个事件，简单地通过事件监听就可以获取到相关的设备运动信息<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">window</span>.addEventListener(<span class="string">"devicemotion"</span>, <span class="function"><span class="keyword">function</span>(<span class="params">event</span>) </span>&#123;&#125;, <span class="literal">false</span>);</span><br></pre></td></tr></table></figure><br>最简单的设备运动，我们基本上可以认为是前后、左右、上下这么6个方向上的，装换成3D环境中来讲的话，就是在x、y和z轴上运动，这三个轴上的信息，都包含在了event对象中。我们可以通过这种方式获得</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">window</span>.addEventListener(<span class="string">"devicemotion"</span>, <span class="function"><span class="keyword">function</span>(<span class="params">event</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">var</span> eventacceleration = event.acceleration;</span><br><span class="line">    <span class="built_in">document</span>.querySelector(<span class="string">'#devicemotion'</span>).innerHTML = <span class="string">"acceleration:&lt;br&gt;"</span>+</span><br><span class="line">    eventacceleration.x+<span class="string">"&lt;br&gt;"</span>+</span><br><span class="line">    eventacceleration.y+<span class="string">"&lt;br&gt;"</span>+</span><br><span class="line">    eventacceleration.z</span><br><span class="line">&#125;, <span class="literal">false</span>);</span><br></pre></td></tr></table></figure>
<p>不要问我为什么手机躺在桌面上，上面的数字还在不停地抖动，咱们文化人应该都知道什么叫相对静止。上面的三个不断变化的数字，就是你设备的运动数据了。如果你在惊讶为什么我都已经移动过手机，为什么上面的数字貌似也没什么大的变化~那么我会告诉你说：有本事你的眼睛跟着你的手机一样快速移动啊，运动过程中你就能发现这个值的变化了。没有这个本事吧！~自己尝试修改下那个demo。把运动中值的变化都记录下来看看。。。（PS：反正我不弄。）</p>
<p>还有一种比较理想的设备运动方式就是不产生位移，只是快速的旋转手机。这个旋转的信息同样也被反馈在了event对象中</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">window</span>.addEventListener(<span class="string">"devicemotion"</span>, <span class="function"><span class="keyword">function</span>(<span class="params">event</span>) </span>&#123;</span><br><span class="line">    eventrotationRate = event.rotationRate;</span><br><span class="line">    <span class="built_in">document</span>.querySelector(<span class="string">'#devicemotion'</span>).innerHTML = <span class="string">'rotationRate:&lt;br&gt;'</span>+</span><br><span class="line">    eventrotationRate.alpha+<span class="string">'&lt;br&gt;'</span>+</span><br><span class="line">    eventrotationRate.beta+<span class="string">"&lt;br&gt;"</span>+</span><br><span class="line">    eventrotationRate.gamma</span><br><span class="line">&#125;, <span class="literal">false</span>);</span><br></pre></td></tr></table></figure>
<p>可能有的童鞋会觉得~有了这个旋转应该就能实现一些手机左右旋转产生视差的效果了，其实是不行的~因为这货和上面的一样，设备停止后，我们可以认为他的值归0~。真正想要实现手机旋转视差，我们需要用到的就是另外一个event的属性accelerationIncludingGravity。从字面上理解这个属性就是重力加速度。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">window</span>.addEventListener(<span class="string">"devicemotion"</span>, <span class="function"><span class="keyword">function</span>(<span class="params">event</span>) </span>&#123;</span><br><span class="line">    eventaccelerationIncludingGravity = event.accelerationIncludingGravity;</span><br><span class="line">    <span class="built_in">document</span>.querySelector(<span class="string">'#devicemotion'</span>).innerHTML = <span class="string">"accelerationIncludingGravity:&lt;br&gt;"</span>+</span><br><span class="line">    eventaccelerationIncludingGravity.x+<span class="string">"&lt;br&gt;"</span>+</span><br><span class="line">    eventaccelerationIncludingGravity.y+<span class="string">"&lt;br&gt;"</span>+</span><br><span class="line">    eventaccelerationIncludingGravity.z</span><br><span class="line">&#125;, <span class="literal">false</span>);</span><br></pre></td></tr></table></figure><br>其实，devicemotion还有一个好基友，这里也推荐给大家看一眼吧：</p>
<p><strong>deviceorientation</strong><br>这个事件和devicemotion的使用方法基本一致<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">window</span>.addEventListener(<span class="string">"deviceorientation"</span>, <span class="function"><span class="keyword">function</span>(<span class="params">event</span>) </span>&#123;</span><br><span class="line">    <span class="built_in">document</span>.querySelector(<span class="string">'#deviceorientation'</span>).innerHTML = </span><br><span class="line">    event.alpha+<span class="string">'&lt;br&gt;'</span>+</span><br><span class="line">    event.beta+<span class="string">"&lt;br&gt;"</span>+</span><br><span class="line">    event.gamma+<span class="string">'&lt;br&gt;'</span>;</span><br><span class="line">&#125;, <span class="literal">false</span>);</span><br></pre></td></tr></table></figure></p>
<p>有前面devicemotion好基友的帮忙，似乎能做的东西又可以更多一点了。咱们前面貌似提及过&ldquo;喝前摇一摇&rdquo;。摇完之后，自然是要打开瓶盖痛饮一番了，结合旋转加速度，我们是不是可以尝试些一个拧瓶盖比赛的游戏了？怕长胖？要不，拧个螺丝钉也行。</p>
<h2 id="Websocket"><a href="#Websocket" class="headerlink" title="Websocket"></a>Websocket</h2><p>浏览器与服务器全双工通信(full-duplex)。</p>
<p><strong>核心代码</strong> ：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> ws = <span class="keyword">new</span> WebSocket(<span class="string">'ws://127.0.0.1:8808/'</span>);<span class="comment">//建立服务器连接ws.onopen = function()&#123;</span></span><br><span class="line">	systemInfo.innerHTML = <span class="string">'&lt;p&gt;和websocket服务器连接成功&lt;/p&gt;'</span>;</span><br><span class="line">&#125;<span class="comment">//接收到服务器返回的数据ws.onmessage = function(e)&#123;</span></span><br><span class="line">	systemInfo.innerHTML += <span class="string">'&lt;p&gt;'</span>+e.data+<span class="string">'&lt;/p&gt;'</span>;</span><br><span class="line">&#125;<span class="comment">//断开服务器连接ws.onclose = function()&#123;</span></span><br><span class="line">	systemInfo.innerHTML += <span class="string">'&lt;p&gt;WebSocket服务器连接关闭&lt;/p&gt;'</span>;</span><br><span class="line">&#125;<span class="comment">//ws发生错误ws.onerror = function(e)&#123;</span></span><br><span class="line">	<span class="built_in">console</span>.log(e);</span><br><span class="line">	systemInfo.innerHTML += <span class="string">'&lt;p&gt;WebSocket发生错误&lt;/p&gt;'</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p> 我们可以通过事件的监听，去实时获取到网络状态的变化<br> <figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">window</span>.addEventListener(<span class="string">'offline'</span>, <span class="function"><span class="keyword">function</span>(<span class="params">e</span>) </span>&#123;alert(<span class="string">'offline'</span>);&#125;)</span><br><span class="line"><span class="built_in">window</span>.addEventListener(<span class="string">'online'</span>, <span class="function"><span class="keyword">function</span>(<span class="params">e</span>) </span>&#123;alert(<span class="string">'online'</span>);&#125;)</span><br></pre></td></tr></table></figure></p>
<h2 id="NFC"><a href="#NFC" class="headerlink" title="NFC"></a>NFC</h2><p>近场通信/近距离无线通信技术。</p>
<p>其实说实在的，这个功能我并没有在我的页面里面调试出来。主要的一个原因是~这货目前只是在firefox os系统里面的firefox浏览器里面实现了，手头上没这设备。不过从他们官网的例子中，我大概地可以感受得到这货的好用，有兴趣的同学自行前往学习：[<a href="https://developer.mozilla.org/en-US/docs/Web/API/NFC_API/Using_the_NFC_API]https://developer.mozilla.org/en-US/docs/Web/API/NFC_API/Using_the_NFC_API" target="_blank" rel="noopener">https://developer.mozilla.org/en-US/docs/Web/API/NFC_API/Using_the_NFC_API]https://developer.mozilla.org/en-US/docs/Web/API/NFC_API/Using_the_NFC_API</a>.</p>
<h2 id="震动-Vibration-API"><a href="#震动-Vibration-API" class="headerlink" title="震动 - Vibration API"></a>震动 - Vibration API</h2><p>设备震动</p>
<p><strong>核心代码</strong> :<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">navigator.vibrate = navigator.vibrate ||  navigator.webkitVibrate || </span><br><span class="line">                                navigator.mozVibrate || navigator.msVibrate;</span><br><span class="line">navigator.vibrate(value);</span><br><span class="line">navigator.vibrate(<span class="number">0</span>);</span><br></pre></td></tr></table></figure><br>vibrate的参数为震动的时间，</p>
<p>如果值为0，说明停止震动</p>
<p>值为一个数组的话，奇数项表示的是震动时间，偶数项表示的为间隔时间。</p>
<p>比如：vibrate(1000,1000,2000)  就是表示震动1秒之后暂停1秒，再震动2秒</p>
<h2 id="网络环境-Connection-API"><a href="#网络环境-Connection-API" class="headerlink" title="网络环境 Connection API"></a>网络环境 Connection API</h2>
            <!--[if lt IE 9]><script>document.createElement('audio');</script><![endif]-->
            <audio id="audio" loop="1" preload="auto" controls="controls"
                data-autoplay="false">
                <source type="audio/mpeg" src="">
            </audio>
            
            <ul id="audio-list" style="display:none">
                
                
                <li title='0' data-url='/statics/chengdu.mp3'></li>
                
                    
            </ul>
            
            
            
    <div id='gitalk-container' class="comment link"
        data-ae='true'
        data-ci='ec894e2b66f752e8b7fb'
        data-cs='3ccc2e92bb350688fe2c2dc2930189b62622bfb1'
        data-r='blog-comments'
        data-o='TriDiamond'
        data-a='TriDiamond'
        data-d='undefined'
    >Comments</div>


            
            
        </div>
        <div class="sidebar">
            <div class="box animated fadeInRight">
                <div class="subbox">
                    <img src="https://res.cloudinary.com/tridiamond/image/upload/v1573019751/TriDiamond_logo_ui_xeublz.jpg" height=300 width=300></img>
                    <p>张白告丶</p>
                    <span>Think like an artist, develop like an artisan</span>
                    <dl>
                        <dd><a href="https://github.com/zhanghao-web" target="_blank"><span
                                    class=" iconfont icon-github"></span></a></dd>
                        <dd><a href="" target="_blank"><span
                                    class=" iconfont icon-twitter"></span></a></dd>
                        <dd><a href="" target="_blank"><span
                                    class=" iconfont icon-stack-overflow"></span></a></dd>
                    </dl>
                </div>
                <ul>
                    <li><a href="/">149 <p>Articles</p></a></li>
                    <li><a href="/categories">23 <p>Categories</p></a></li>
                    <li><a href="/tags">47 <p>Tags</p></a></li>
                </ul>
            </div>
            
            
            
            <div class="box sticky animated fadeInRight faster">
                <div id="toc" class="subbox">
                    <h4>Contents</h4>
                    <ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#HTML5-Device-API"><span class="toc-number">1.</span> <span class="toc-text">HTML5 Device API</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#让音乐随心而动-音频处理-Web-audio-API"><span class="toc-number">1.1.</span> <span class="toc-text">让音乐随心而动 - 音频处理 Web audio API</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1-源节点（source-node）"><span class="toc-number">1.1.1.</span> <span class="toc-text">1.源节点（source node）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-分析节点（analyser-node）"><span class="toc-number">1.1.2.</span> <span class="toc-text">2.分析节点（analyser node）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-处理节点（gain-node、panner-node、wave-shaper-node、delay-node、convolver-node等）"><span class="toc-number">1.1.3.</span> <span class="toc-text">3.处理节点（gain node、panner node、wave shaper node、delay node、convolver node等）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-目的节点（destination-node）"><span class="toc-number">1.1.4.</span> <span class="toc-text">4.目的节点（destination node）</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#捕捉用户摄像头-媒体流-Media-Capture"><span class="toc-number">1.2.</span> <span class="toc-text">捕捉用户摄像头 - 媒体流 Media Capture</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1、摄像头捕捉"><span class="toc-number">1.2.1.</span> <span class="toc-text">1、摄像头捕捉</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2、从视频流中拍照"><span class="toc-number">1.2.2.</span> <span class="toc-text">2、从视频流中拍照</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3、用户声音录制"><span class="toc-number">1.2.3.</span> <span class="toc-text">3、用户声音录制</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4、保存用户录制的声音"><span class="toc-number">1.2.4.</span> <span class="toc-text">4、保存用户录制的声音</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#你是逗逼？-语音识别-Web-Speech-API"><span class="toc-number">1.3.</span> <span class="toc-text">你是逗逼？ - 语音识别 Web Speech API</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1、文本转换成语音，使用SpeechSynthesisUtterance对象"><span class="toc-number">1.3.1.</span> <span class="toc-text">1、文本转换成语音，使用SpeechSynthesisUtterance对象.</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2、语音转换为文本，使用SpeechRecognition对象"><span class="toc-number">1.3.2.</span> <span class="toc-text">2、语音转换为文本，使用SpeechRecognition对象.</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#让我尽情呵护你-设备电量-Battery-API"><span class="toc-number">1.4.</span> <span class="toc-text">让我尽情呵护你 - 设备电量 Battery API</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#获取用户位置-地理位置-Geolocation-API"><span class="toc-number">1.5.</span> <span class="toc-text">获取用户位置 - 地理位置 Geolocation API</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#把用户捧在手心-环境光-Ambient-Light-API"><span class="toc-number">1.6.</span> <span class="toc-text">把用户捧在手心 - 环境光 Ambient Light API</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#陀螺仪-Deviceorientation"><span class="toc-number">1.7.</span> <span class="toc-text">陀螺仪 Deviceorientation</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Websocket"><span class="toc-number">1.8.</span> <span class="toc-text">Websocket</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#NFC"><span class="toc-number">1.9.</span> <span class="toc-text">NFC</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#震动-Vibration-API"><span class="toc-number">1.10.</span> <span class="toc-text">震动 - Vibration API</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#网络环境-Connection-API"><span class="toc-number">1.11.</span> <span class="toc-text">网络环境 Connection API</span></a></li></ol></li></ol>
                </div>
            </div>
            
            
        </div>
    </div>
</div>

    </div>
</div>
    <div id="back-to-top" class="animated fadeIn faster">
        <div class="flow"></div>
        <span class="percentage animated fadeIn faster">0%</span>
        <span class="iconfont icon-top02 animated fadeIn faster"></span>
    </div>
</body>
<footer>
    <p class="copyright" id="copyright">
        &copy; 2020
        <span class="gradient-text">
            张白告丶
        </span>.
        Powered by <a href="http://hexo.io/" title="Hexo" target="_blank" rel="noopener">Hexo</a>
        Theme
        <span class="gradient-text">
            <a href="https://github.com/TriDiamond/hexo-theme-obsidian" title="Obsidian" target="_blank" rel="noopener">Obsidian</a>
        </span>
        <small><a href="https://github.com/TriDiamond/hexo-theme-obsidian/blob/master/CHANGELOG.md" title="v1.4.3" target="_blank" rel="noopener">v1.4.3</a></small>
    </p>
</footer>

<script type="text/javascript" src="https://cdn.bootcss.com/mathjax/2.7.6/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>
<script>
  MathJax.Hub.Config({
    "HTML-CSS": {
      preferredFont: "TeX",
      availableFonts: ["STIX", "TeX"],
      linebreaks: {
        automatic: true
      },
      EqnChunk: (MathJax.Hub.Browser.isMobile ? 10 : 50)
    },
    tex2jax: {
      inlineMath: [
        ["$", "$"],
        ["\\(", "\\)"]
      ],
      processEscapes: true,
      ignoreClass: "tex2jax_ignore|dno",
      skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
    },
    TeX: {
      noUndefined: {
        attributes: {
          mathcolor: "red",
          mathbackground: "#FFEEEE",
          mathsize: "90%"
        }
      },
      Macros: {
        href: "{}"
      }
    },
    messageStyle: "none"
  });
</script>
<script>
  function initialMathJax() {
    MathJax.Hub.Queue(function () {
      var all = MathJax.Hub.getAllJax(),
        i;
      // console.log(all);
      for (i = 0; i < all.length; i += 1) {
        console.log(all[i].SourceElement().parentNode)
        all[i].SourceElement().parentNode.className += ' has-jax';
      }
    });
  }

  function reprocessMathJax() {
    if (typeof MathJax !== 'undefined') {
      MathJax.Hub.Queue(["Typeset", MathJax.Hub]);
    }
  }
</script>



    
<link rel="stylesheet" href="//cdn.bootcss.com/gitalk/1.5.0/gitalk.min.css">

    
<script src="//cdn.bootcss.com/gitalk/1.5.0/gitalk.min.js"></script>



<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="/js/plugin.js"></script>
<script src="/js/obsidian.js"></script>
<script src="/js/jquery.truncate.js"></script>
<script src="/js/search.js"></script>


<script src="//cdn.bootcss.com/typed.js/2.0.10/typed.min.js"></script>


<script src="//cdn.bootcss.com/blueimp-md5/2.12.0/js/md5.min.js"></script>


<script src="//cdnjs.cloudflare.com/ajax/libs/social-share.js/1.0.16/js/social-share.min.js"></script>


<script src="https://cdn.bootcss.com/codemirror/5.48.4/codemirror.min.js"></script>

    
<script src="//cdn.bootcss.com/codemirror/5.48.4/mode/javascript/javascript.min.js"></script>


    
<script src="//cdn.bootcss.com/codemirror/5.48.4/mode/css/css.min.js"></script>


    
<script src="//cdn.bootcss.com/codemirror/5.48.4/mode/xml/xml.min.js"></script>


    
<script src="//cdn.bootcss.com/codemirror/5.48.4/mode/htmlmixed/htmlmixed.min.js"></script>


    
<script src="//cdn.bootcss.com/codemirror/5.48.4/mode/clike/clike.min.js"></script>


    
<script src="//cdn.bootcss.com/codemirror/5.48.4/mode/php/php.min.js"></script>


    
<script src="//cdn.bootcss.com/codemirror/5.48.4/mode/shell/shell.min.js"></script>


    
<script src="//cdn.bootcss.com/codemirror/5.48.4/mode/python/python.min.js"></script>




    
<script src="/js/busuanzi.min.js"></script>

    <script>
        $(document).ready(function () {
            if ($('span[id^="busuanzi_"]').length) {
                initialBusuanzi();
            }
        });
    </script>



<link rel="stylesheet" href="//cdn.bootcss.com/photoswipe/4.1.3/photoswipe.min.css">
<link rel="stylesheet" href="//cdn.bootcss.com/photoswipe/4.1.3/default-skin/default-skin.min.css">


<script src="//cdn.bootcss.com/photoswipe/4.1.3/photoswipe.min.js"></script>
<script src="//cdn.bootcss.com/photoswipe/4.1.3/photoswipe-ui-default.min.js"></script>


<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">
    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>
    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">
        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>
        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">
            <div class="pswp__top-bar">
                <!--  Controls are self-explanatory. Order can be changed. -->
                <div class="pswp__counter"></div>
                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>
                <button class="pswp__button pswp__button--share" title="Share"></button>
                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>
                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>
                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                      <div class="pswp__preloader__cut">
                        <div class="pswp__preloader__donut"></div>
                      </div>
                    </div>
                </div>
            </div>
            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div> 
            </div>
            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>
            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>
            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>
        </div>
    </div>
</div>



    <!-- Global site tag (gtag.js) - Google Analytics -->
    <script async src="//www.googletagmanager.com/gtag/js?id=UA-149874671-1"></script>
    <script>
        window.dataLayer = window.dataLayer || [];
        function gtag(){dataLayer.push(arguments);}
        gtag('js', new Date());

        gtag('config', 'UA-149874671-1');
    </script>





<script>
    function initialTyped () {
        var typedTextEl = $('.typed-text');
        if (typedTextEl && typedTextEl.length > 0) {
            var typed = new Typed('.typed-text', {
                strings: ["Think like an artist, develop like an artisan", "艺术家思维去思考问题，工匠创造精神去开发"],
                typeSpeed: 90,
                loop: true,
                loopCount: Infinity,
                backSpeed: 20,
            });
        }
    }

    if ($('.article-header') && $('.article-header').length) {
        $(document).ready(function () {
            initialTyped();
        });
    }
</script>




</html>
